<template>
	<view class="searchLayout">
		<view class="search">
			<uni-search-bar @confirm="onSearch" @cancel="onClear" @clear="onClear" focus placeholder="搜索"
				v-model="queryParams.keyword">
			</uni-search-bar>
		</view>

		<view v-if="!classList.length||noSearch">
			<view class="history">
				<view class="topTitle">
					<view class="text">最近搜索</view>
					<view class="icon" @click="removeHistory">
						<uni-icons type="trash" size="25"></uni-icons>
					</view>
				</view>
				<view class="tabs">
					<view class="tab" v-for="tab in historySearch" :key="tab" @click="clickTab(tab)">{{tab}}</view>
				</view>
			</view>

			<view class="recommend">
				<view class="topTitle">
					<view class="text">热门搜索</view>
				</view>
				<view class="tabs">
					<view class="tab" v-for="tab in recommendList" :key="tab" @click="clickTab(tab)">{{tab}}</view>
				</view>
			</view>
		</view>

		<view class="noSearch" v-if="noSearch">
			<uv-empty mode="search" icon="http://cdn.uviewui.com/uview/empty/search.png"></uv-empty>
		</view>

		<view v-else>
			<view class="list">
				<navigator :url="`/pages/preview/preview?id=${item._id}`" class="item" v-for="item in classList"
					:key="item._id">
					<image :src="item.smallPicurl" mode="aspectFill"></image>
				</navigator>
			</view>
			<view v-if="noData || classList.length">
				<uni-load-more :status="noData?'noMore':'loading'" />
			</view>
		</view>

		<view class="goTop" @click="onTop">
			<uni-icons type="arrow-up" size="30" color="#fff"></uni-icons>
		</view>

	</view>
</template>

<script setup>
	import {
		ref
	} from "vue";
	import {
		onLoad,
		onUnload,
		onReachBottom
	} from "@dcloudio/uni-app";
	import {
		getSearchWallApi
	} from "../../api/apis";

	//查询参数
	const queryParams = ref({
		pageNum: 1,
		pageSize: 12,
		keyword: ""
	})

	//搜索历史词
	const historySearch = ref(uni.getStorageSync('historySearch') || []);

	//热门搜索词
	const recommendList = ref(["美女", "帅哥", "宠物", "卡通"]);

	//没有更多
	const noData = ref(false);
	//没有搜索结果
	const noSearch = ref(false);

	//搜索结果列表
	const classList = ref([]);


	//点击搜索
	const onSearch = () => {
		queryParams.value.keyword = queryParams.value.keyword.trim()
		console.log(queryParams.value.keyword);
		if (queryParams.value.keyword === '') {
			uni.showModal({
				title: '错误提示',
				content: '查询参数不能为空,请重新输入',
				showCancel: false
			})
			return
		}
		if (queryParams.value.keyword.startsWith('++++')) {
			uni.setStorageSync('token', queryParams.value.keyword.slice(4))
			uni.showToast({
				title: 'token设置成功'
			})
			uni.reLaunch({
				url: '/pages/index/index'
			})
			return
		}
		onClear()
		historySearch.value = [...new Set([queryParams.value.keyword, ...historySearch.value])].slice(0, 10)
		uni.setStorageSync('historySearch', historySearch.value)
		getClassListData()
	}

	const getClassListData = async () => {
		try {
			const res = await getSearchWallApi(queryParams.value)
			classList.value = [...classList.value, ...res.data]
			uni.setStorageSync('classList', classList.value)
			uni.setStorageSync('classify', queryParams.value.keyword)
			if (res.data.length < queryParams.value.pageSize) noData.value = true
			if (res.data.length === 0 && classList.value.length === 0) noSearch = true
		} catch (error) {
			console.log(error);
			noData.value = true
			//TODO handle the exception
		}
	}

	//点击清除按钮
	const onClear = () => {
		classList.value = []
		noData.value = false
		noSearch.value = false
		queryParams.value.pageNum = 1
	}



	//点击标签进行搜索
	const clickTab = (value) => {
		queryParams.value.keyword = value
		onSearch()
	}


	//点击清空搜索记录
	const removeHistory = () => {
		uni.showModal({
			title: "是否清空历史搜索？",
			success: res => {
				if (res.confirm) {
					uni.removeStorageSync('historySearch')
					historySearch.value = []
				}
			}
		})
	}

	//触底加载更多
	onReachBottom(() => {
		if (noData.value) return
		queryParams.value.pageNum++
		getClassListData()
	})

	//关闭有页面
	onUnload(() => {
		uni.removeStorageSync('classList')
		uni.removeStorageSync('classify')
	})

	const onTop = () => {
		uni.pageScrollTo({
			scrollTop: 0
		})
	}
</script>

<style lang="scss" scoped>
	.searchLayout {

		.search {
			padding: 0 10rpx;
		}

		.topTitle {
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 32rpx;
			color: #999;
		}

		.history {
			padding: 30rpx;
		}

		.recommend {
			padding: 30rpx;
		}

		.tabs {
			display: flex;
			align-items: center;
			flex-wrap: wrap;
			padding-top: 20rpx;

			.tab {
				background: #F4F4F4;
				font-size: 28rpx;
				color: #333;
				padding: 10rpx 28rpx;
				border-radius: 50rpx;
				margin-right: 20rpx;
				margin-top: 20rpx;
			}
		}

		.list {
			display: grid;
			grid-template-columns: repeat(3, 1fr);
			gap: 5rpx;
			padding: 20rpx 5rpx;

			.item {
				height: 440rpx;

				image {
					width: 100%;
					height: 100%;
					display: block;
				}
			}
		}

		.goTop {
			position: fixed;
			bottom: 80rpx;
			right: 30rpx;
			width: 80rpx;
			height: 80rpx;
			border-radius: 50%;
			background: rgba(0, 0, 0, 0.3);
			color: white;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}
</style>